package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.dto.ExhibitorDTO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.BondManageDTO;
import com.eastfair.venueservice.dto.BondManagePageQuery;
import com.eastfair.venueservice.dto.BondManageSaveDTO;
import com.eastfair.venueservice.dto.BondManageUpdateDTO;
import com.eastfair.venueservice.entity.BondManage;
import com.eastfair.venueservice.service.BondManageService;
import com.eastfair.venueservice.service.BondPayRecordService;
import com.eastfair.venueservice.vo.BondManageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 保证金管理
 * </p>
 *
 * @author lyw
 * @date 2023-02-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/bondManage")
@Api(value = "BondManage", tags = "保证金管理")
@PreAuth(replace = "venueservice:bondManage:")
public class BondManageController extends SuperController<BondManageService, Long, BondManage, BondManagePageQuery, BondManageSaveDTO, BondManageUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private BondManageService bondManageService;
    @Autowired
    private BondPayRecordService bondPayRecordService;

    @Override
    public void handlerResult(IPage<BondManage> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<BondManage> bondManageList = list.stream().map((map) -> {
            BondManage bondManage = BondManage.builder().build();
            //TODO 请在这里完成转换
            return bondManage;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(bondManageList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<BondManagePageQuery> params) {
        BondManagePageQuery pageQuery = params.getModel();

    }


    /**
     * 分页查询保证金管理列表
     * @param pageParams
     * @return
     */
    @PostMapping("/page/queryBondManageList")
    public R<IPage<BondManageVO>> queryBondManageList(@RequestBody PageParams<BondManageDTO> pageParams) {
        log.info("queryBondManageList - 分页查询保证金管理列表, pageParams={}", pageParams);
        try {
            return R.successDef(bondManageService.queryBondManageList(pageParams));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 通过服务商id获取保证金集合
     * @param exhibitorDTO
     * @return
     */
    @PostMapping("/getBondManageList")
    public R<List<BondManageVO>> getBondManageList(@RequestBody ExhibitorDTO exhibitorDTO) {
        try {
            return R.successDef(bondManageService.getBondManageList(exhibitorDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }


    /**
     * 保存保证金管理
     * @param bondManageDTO
     * @return
     */
    @ApiOperation(value = "保存保证金管理")
    @SysLog("添加保证金")
    @PostMapping(value = "/saveBondManageInfo")
    public R<Boolean> saveBondManageInfo(@RequestBody BondManageDTO bondManageDTO){
        try {
            bondManageService.saveForVenue(bondManageDTO);
            return R.success(true);
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }



    /**
     * 设置保证金管理金额
     * @param bondManageDTO
     * @return
     */
    @ApiOperation(value = "设置保证金管理金额")
    @SysLog("设置保证金")
    @PostMapping(value = "/updateBondManageById")
    public R<Boolean> updateBondManageById(@RequestBody BondManageDTO bondManageDTO){
        try {
            return R.success(bondManageService.updateBondManageById(bondManageDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }


    /**
     * 通过服务商id删除设置保证金信息
     * @param id
     * @return
     */
    @ApiOperation(value = "通过服务商id删除设置保证金信息")
    @SysLog("删除保证金")
    @GetMapping(value = "/delBondManageByServiceProviderId/{id}")
    public R<Boolean> delBondManageByServiceProviderId(@PathVariable Long id){
        log.info("delBondManageByServiceProviderId - 通过服务商id删除设置保证金信息, id={}", id);
        try {
            return R.success(bondManageService.delBondManageByServiceProviderId(id));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }



    /**
     * 通过id查询保证金信息
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询保证金信息")
    @GetMapping(value = "/getBondManageById/{id}")
    public R<BondManageVO> getBondManageById(@PathVariable Long id){
        try {
            return R.success(bondManageService.getBondManageVOById(id));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 订单编号查询保证金订单.
     *
     * @param orderNumber the order number
     * @return the by order number
     */
    @GetMapping("/get/orderNumber/{orderNumber}")
    public R<BondManageVO> getByOrderNumber(@PathVariable("orderNumber") String orderNumber) {
        log.info("getByOrderNumber - 订单编号查询保证金订单, orderNumber={}", orderNumber);
        return R.success(bondManageService.getVOByOrderNumber(orderNumber));
    }

    /**
     * 支付保证金
     * @param bondManageDTO bondManageDTO
     * @return R
     */
    @SysLog("支付保证金")
    @ApiOperation(value = "支付保证金")
    @PostMapping(value = "/saveBondPayRecordInfo")
    public R<Boolean> payBond(@RequestBody BondManageDTO bondManageDTO){
        log.info("saveBondPayRecordInfo - 支付保证金, bondManageDTO={}", bondManageDTO);
        try {
            return R.success(bondPayRecordService.payBond(bondManageDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 更新退款状态
     *
     * @param bondManageDTO the bond manage dto
     * @return the r
     */
    @PostMapping("/update/refundStatus")
    public R<Boolean> updateRefundStatus(@RequestBody BondManageDTO bondManageDTO) {
        log.info("updateRefundStatus - 更新退款状态, bondManageDTO={}", bondManageDTO);
        try {
            bondManageService.updateRefundStatus(bondManageDTO);
            return R.successDef(true);
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
